package cn.s1995.sloth.bms.controller;

import cn.s1995.sloth.bizs.entity.Order;
import cn.s1995.sloth.bizs.oo.req.ChartREQ;
import cn.s1995.sloth.bizs.oo.vo.ChartVO;
import cn.s1995.sloth.bizs.oo.vo.DashboardOrderStateVO;
import cn.s1995.sloth.bizs.oo.vo.DashboardStatisticsVO;
import cn.s1995.sloth.bizs.service.DashboardService;
import cn.s1995.sloth.common.core.constant.enums.hint.Hint;
import cn.s1995.sloth.common.core.util.DateUtil;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.core.util.StringUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * DashboardController
 *
 * @author Lex
 * @description
 * @date 2024/04/30 10:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dashboard")
@Tag(description = "Dashboard", name = "BMS-仪表盘")
public class DashboardController {

    private final DashboardService dashboardService;

    @ApiOperationSupport(order = 10)
    @Operation(summary = "获取统计数据", description = "获取统计数据")
    @GetMapping("/statistics")
    public R<DashboardStatisticsVO> getStatistics() {
        return R.ok(dashboardService.getStatistics());
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "获取图表数据", description = "今日<br/>近七日<br/>")
    @GetMapping("/chart")
    public R<List<ChartVO>> getChart(@ParameterObject ChartREQ req) {
        if (req.getType() == null) {
            return R.failed(Hint.BAD_PARAMETER);
        }
        if (StringUtil.isEmpty(req.getUnit())) {
            req.setUnit(DateUtil.Unit.day);
        }
        return R.ok(dashboardService.getChart(req));
    }

    @ApiOperationSupport(order = 30)
    @Operation(summary = "获取最新订单", description = "获取最新订单")
    @Parameter(name = "limit", description = "数量", required = false)
    @GetMapping("/latest-orders")
    public R<List<Order>> getLatestOrders(@RequestParam(required = false, defaultValue = "10") Long limit) {
        return R.ok(dashboardService.getLatestOrders(limit));
    }

    @ApiOperationSupport(order = 31)
    @Operation(summary = "获取订单状态", description = "获取订单状态")
    @GetMapping("/state-orders")
    public R<DashboardOrderStateVO> getStateOrders(@ParameterObject ChartREQ req) {
        return R.ok(dashboardService.getStateOrders(req));
    }

    @ApiOperationSupport(order = 40)
    @Operation(summary = "获取排行榜", description = "获取排行榜")
    @Parameters({
        @Parameter(name = "type", description = "类型：1.产品 2.店铺", required = true),
        @Parameter(name = "limit", description = "数量", required = false),
    })
    @GetMapping("/ranking-list")
    public R<List> getRankingList(@RequestParam Integer type,
                                  @RequestParam(required = false, defaultValue = "10") Long limit) {
        return R.ok(dashboardService.getRankingList(type, limit));
    }

}
